package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.alarm.AlarmRuleService;
import com.alibaba.otter.manager.biz.config.alarm.dto.AlarmRuleQueryDTO;
import com.alibaba.otter.manager.biz.config.parameter.SystemParameterService;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.AlarmRuleAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.autokeeper.AutoKeeperCluster;
import com.alibaba.otter.shared.common.model.config.alarm.AlarmRule;
import com.mybatisflex.core.paginate.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/api/v1/alarm-rule")
@RestController
public class AlarmRuleResource {


    private final AlarmRuleService alarmRuleService;


    private final AlarmRuleAggService alarmRuleAggService;


    public AlarmRuleResource(AlarmRuleService alarmRuleService, AlarmRuleAggService alarmRuleAggService) {
        this.alarmRuleService = alarmRuleService;
        this.alarmRuleAggService = alarmRuleAggService;
    }


    @GetMapping("find-alarm-rules")
    public RestResponse<List<AlarmRule>> findAlarmRules(AlarmRuleQueryDTO queryDTO, Pageable pageable) {
        return PaginationUtil.pageHttpResponse(alarmRuleService.pageByQuery(queryDTO,pageable));
    }



    /**
     * 根据id查询zookeeper信息
     */
    @GetMapping("findOne/{id}")
    public RestResponse<AlarmRule> findOne(@PathVariable("id") Long id) {
        return RestResponse.ok(alarmRuleService.getAlarmRuleById(id));
    }


    /**
     * 添加告警规则
     */
    @PostMapping("create")
    public RestResponse<Void> add(@RequestBody @Validated AlarmRule alarmRule) {
        alarmRuleService.create(alarmRule);
        return RestResponse.ok();
    }


    /**
     * 修改告警规则
     */
    @PutMapping("modify")
    public RestResponse<Void> modify(@RequestBody @Validated AlarmRule alarmRule) {
        alarmRuleService.modify(alarmRule);
        return RestResponse.ok();
    }


    /**
     * 删除告警规则
     */
    @DeleteMapping("delete/{alarmRuleId}")
    public RestResponse<Void> delete(@PathVariable("alarmRuleId") Long alarmRuleId) {
        alarmRuleService.remove(alarmRuleId);
        return RestResponse.ok();
    }

    /**
     * 一键添加监控规则
     */
    @PostMapping("one-key-create-monitor/{pipelineId}")
    public RestResponse<Void> oneKeyAddMonitor(@PathVariable("pipelineId") Long pipelineId) {
        alarmRuleService.oneKeyAddMonitor(pipelineId);
        return RestResponse.ok();
    }




    /**
     * 状态变更
     */
    @PutMapping("modify-status")
    public RestResponse<Void> modifyStatus(@RequestParam("alarmRuleId") Long alarmRuleId,
                                           @RequestParam("status") String status,
                                           @RequestParam(value = "pauseTime", required = false) String pauseTime) {
        alarmRuleAggService.statusByRule(status, alarmRuleId, pauseTime);
        return RestResponse.ok();
    }

    @PutMapping("status-by-pipeline-id")
    public RestResponse<Void> StatusByPipeline(@RequestParam("pipelineId") Long pipelineId,
                                               @RequestParam("status") String status,
                                               @RequestParam(value = "pauseTime", required = false) String pauseTime) {

        alarmRuleAggService.statusByPipelineId(pipelineId,status,pauseTime);
        return RestResponse.ok();
    }

    @PutMapping("status-by-rule-ids")
    public RestResponse<Void> statusByRule(@RequestParam("alarmRuleIds") String alarmRuleIds,
                                           @RequestParam("status") String status,
                                           @RequestParam(value = "pauseTime", required = false) String pauseTime) {

        alarmRuleAggService.statusByRule(alarmRuleIds, status, pauseTime);
        return RestResponse.ok();
    }

    @PostMapping("one-key-monitor")
    public RestResponse<Void> oneKeyMonitor(@RequestParam("pipelineId") Long pipelineId) {
        alarmRuleService.oneKeyAddMonitor(pipelineId);
        return RestResponse.ok();
    }


}
